package com.wang.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.to.MemberPrice;
import com.wang.common.to.SkuReductionTo;
import com.wang.common.utils.PageUtils;
import com.wang.common.utils.Query;
import com.wang.gulimall.coupon.dao.SkuFullReductionDao;
import com.wang.gulimall.coupon.entity.MemberPriceEntity;
import com.wang.gulimall.coupon.entity.SkuFullReductionEntity;
import com.wang.gulimall.coupon.entity.SkuLadderEntity;
import com.wang.gulimall.coupon.service.MemberPriceService;
import com.wang.gulimall.coupon.service.SkuFullReductionService;
import com.wang.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku的满减信息
     * @param skuReductionTo
     */
    @Override
    public void saveReduction(SkuReductionTo skuReductionTo) {
        //5.4 保存sku的优惠、满减等信息
        // 这个涉及其他的数据库 gulimall_sms
        // sms_sku_ladder:几件打几折 阶梯价格
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        //设置skuid
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        //设置满多少件打折
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        //设置打几折
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        //设置是否叠加其他优惠
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        //设置打折后的价格 这里先不设置，在下单的时候在设定
        //保存前筛选垃圾信息 执行保存
        if (skuLadderEntity.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }

        // sms_sku_full_reduction：满减信息，满几件打几折
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        //这里直接使用属性对拷
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        //保存前筛选垃圾信息 执行保存
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            this.save(skuFullReductionEntity);
        }

        // sms_member_price：商品不同会员登记对应的价格
        List<MemberPrice> memberPriceList = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPriceList.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            //设置skuid
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            //设置会员等级
            memberPriceEntity.setMemberLevelId(item.getId());
            //设置会员等级的名字
            memberPriceEntity.setMemberLevelName(item.getName());
            //当前等级对用的价格
            memberPriceEntity.setMemberPrice(item.getPrice());
            //设置是否叠加其他优惠
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        })
                .filter(item->{
                    //价格需要大于0
                    return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
                })
                .collect(Collectors.toList());
        memberPriceService.saveBatch(collect);

    }

}